package com.gaowl.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.gaowl.gulimall.product.entity.ProductAttrValueEntity;
import com.gaowl.gulimall.product.service.ProductAttrValueService;
import com.gaowl.gulimall.product.vo.AttrResponseVo;
import com.gaowl.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gaowl.gulimall.product.entity.AttrEntity;
import com.gaowl.gulimall.product.service.AttrService;
import com.gaowl.common.utils.PageUtils;
import com.gaowl.common.utils.R;



/**
 * 商品属性
 *
 * @author gaowl
 * @email gaowl@stu.jiangnan.edu.cn
 * @date 2021-03-11 13:20:43
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    /**
     * 获取spu规格
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R getBaseAttrListForSpu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> productAttrValueEntityList= productAttrValueService.getBaseAttrListForSpu(spuId);
        return R.ok().put("data", productAttrValueEntityList);
    }

    /**
     * 修改spu规格
     */
    @PostMapping("/update/{spuId}")
    public R updateAttrForSpu(@PathVariable("spuId") Long spuId,
                              @RequestBody List<ProductAttrValueEntity> productAttrValueEntityList){
        productAttrValueService.updateAttrForSpu(spuId, productAttrValueEntityList);
        return R.ok();
    }


    /**
     * 获取分类规格参数
     * @param params 分页参数
     * @param catelogId 三级分类的类别id
     *
     *   attr_type=1，规格参数；0，销售属性；2，即是规格参数也是销售属性
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R getClsSpecification(@RequestParam Map<String, Object> params,
                                 @PathVariable("catelogId") Long catelogId,
                                 @PathVariable("attrType") String attrType){
        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, attrType);
        return R.ok().put("page", page);
    }

    /**
     * 分页查询 属性列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 查询 属性 的详细信息（包含分组id、分类id、分类路径）
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		//AttrEntity attr = attrService.getById(attrId);
        AttrResponseVo attrResponseVo = attrService.getDetailedInfoById(attrId);

        return R.ok().put("attr", attrResponseVo);
    }

    /**
     * 保存 商品属性 的详细信息
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
        attrService.saveAttrVo(attr);

        return R.ok();
    }

    /**
     * 更新 商品属性 的详细信息
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attrVo){
		attrService.updateAttrVo(attrVo);

        return R.ok();
    }

    /**
     * 删除属性信息
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
